/*! ******************************************************************************
 *
 * Created on 2021年9月4日
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/
package org.pentaho.di.ui.spoon.navigation;

import org.eclipse.swt.widgets.Shell;
import org.pentaho.di.core.logging.LogChannelInterface;
import org.yzbdl.lanius.util.LogUtil;
import org.pentaho.di.core.util.Utils;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.ui.spoon.Spoon;
import org.pentaho.di.ui.spoon.delegates.SpoonDelegates;
import org.pentaho.di.ui.util.PinyinUtils;

/**
 * spoon委托类, 放置一些公共方法.
 * 
 * @author hs@yzbdl.ac.cn
 *
 */
public class SpoonRef {

    /**
     * 持有对Spoon的引用
     */
    private Spoon spoon;

    public SpoonRef(Spoon spoon) {
        super();
        this.spoon = spoon;
    }

    public Spoon getSpoon() {
        return spoon;
    }

    public void setSpoon(Spoon spoon) {
        this.spoon = spoon;
    }

    public void setShellText() {
        this.getSpoon().setShellText();
    }

    public void enableMenus() {
        this.getSpoon().enableMenus();
    }

    public LogChannelInterface getLog() {
        return this.getSpoon().getLog();
    }

    /**
     * 委托org.pentaho.di.ui.spoon.Spoon#getShell()
     * 
     * @return
     * @see org.pentaho.di.ui.spoon.Spoon#getShell()
     */
    public Shell getShell() {
        return this.getSpoon().getShell();
    }

    /**
     * 委托org.pentaho.di.ui.spoon.Spoon#rep
     * 
     * @return
     * @see org.pentaho.di.ui.spoon.Spoon#rep
     */
    public Repository getRep() {
        return this.getSpoon().rep;
    }

    /**
     * 委托org.pentaho.di.ui.spoon.Spoon#setRepository()
     * 
     * @return
     * @see org.pentaho.di.ui.spoon.Spoon#setRepository()
     */
    public void setRepository(Repository rep) {
        this.getSpoon().setRepository(rep);
    }

    /**
     * 委托org.pentaho.di.ui.core.PropsUI#isOnlyActiveFileShownInTree方法
     * 
     * @return
     * @see org.pentaho.di.ui.core.PropsUI#isOnlyActiveFileShownInTree()
     */
    public boolean isOnlyActiveFileShownInTree() {
        return this.getSpoon().props.isOnlyActiveFileShownInTree();
    }

    /**
     * 委托Spoon#getActiveTransformation方法
     * 
     * @return
     * @see org.pentaho.di.ui.spoon.Spoon#getActiveTransformation()
     */
    public TransMeta getActiveTransformation() {
        return this.getSpoon().getActiveTransformation();
    }

    /**
     * 委托Spoon#getActiveJob方法
     * 
     * @return
     * @see org.pentaho.di.ui.spoon.Spoon#getActiveJob()
     */
    public JobMeta getActiveJob() {
        return this.getSpoon().getActiveJob();
    }

    /**
     * 获取Spoon#delegates属性
     * 
     * @return
     * @see org.pentaho.di.ui.spoon.Spoon#delegates
     */
    public SpoonDelegates getDelegates() {
        return this.getSpoon().delegates;
    }

    /**
     * 判断传入参数与selectionFilter值是否匹配
     * 
     * @param string
     * @return
     */
    public boolean filterMatch(String string) {
        if (Utils.isEmpty(string)) {
            return true;
        }

        String filter = this.getSpoon().selectionFilter.getText();

        if (Utils.isEmpty(filter)) {
            return true;
        }
        PinyinUtils pinyinUtils = new PinyinUtils();

        String pinyin = pinyinUtils.getPinyin(string).toLowerCase();
        String pinyinFilter = pinyinUtils.getPinyin(filter).toLowerCase();
        String fisrtSpell = pinyinUtils.getFisrtSpell(string).toLowerCase();
        try {
            if (string.matches(filter)) {
                return true;
            }
            if (pinyin.matches(pinyinFilter) | fisrtSpell.matches(pinyinFilter)) {
                return true;
            }
        } catch (Exception e) {
            LogUtil.error("Not a valid pattern [" + filter + "] : " + e.getMessage());
        }
        return pinyin.toUpperCase().contains(pinyinFilter.toUpperCase())
            | fisrtSpell.toUpperCase().contains(pinyinFilter.toUpperCase());
    }
}
